/************************************************************************/
/* Game                                                                 */
/************************************************************************/
#include "Game/Game Objects/Towers/Shots/BaseShot.h"

namespace TD_Game {

	/************************************************************************/
	/* Default values                                                       */
	/************************************************************************/
	const float BaseShot::defaultVelocity = 12/30.0f;
	const float BaseShot::shotCollisionThreshold = 5.0f;
	const unsigned int BaseShot::defaultShotDamage = 7;
	
	/************************************************************************/
	/* ctors and dtors                                                      */
	/************************************************************************/
	BaseShot::BaseShot() {
		m_maxVelocity = defaultVelocity;
	}

	BaseShot::BaseShot(const BaseShot &rhs) {
		
	}

	BaseShot & BaseShot::operator =(const BaseShot &rhs) {
		BaseEntity::operator =(rhs);
		return(*this);
	}

	BaseShot::BaseShot(const Coord2D &initialPosition, unsigned int g_shotDamage, const BaseMario *targetMario, unsigned int marioID) : BaseEntity(initialPosition) {
		m_pTargetMario = targetMario;
		m_marioID = marioID;
	
		/************************************************************************/
		/* Variable initialization                                              */
		/************************************************************************/
		m_maxVelocity	= defaultVelocity;
		m_currShotState	= moving;
		m_hittedTarget	= false;
		m_shotDamage = g_shotDamage;
	}

	BaseShot::~BaseShot() {
		Utils::safeDelete<AnimationSet>(m_pAnimationSet);
	}

	void BaseShot::processStates() {
		if(MarioManager::getInstance()->getMarioByID(m_marioID) != NULL) {
			m_marioPosition = m_pTargetMario->getPosition();
		} 
		
		if(Coord2D::DistanceBetween(m_marioPosition, m_position) < shotCollisionThreshold) {
			if(!m_hittedTarget) {
				hitTarget();
				m_pAnimationSet->setCurrentAnimation("Exploding");
				m_currShotState = exploding;
				m_hittedTarget = true;
			}
		}
		
	}

	void BaseShot::update(unsigned int deltaTime) {
	
		processStates();
		
		if(m_currShotState == moving) {
			//Updating position
			Coord2D velocityVector = (m_marioPosition - m_position);
			float normalizedX = velocityVector.x / velocityVector.getModulus();
			float normalizedY = velocityVector.y / velocityVector.getModulus();
			
			m_position.x += static_cast<int>(normalizedX * m_maxVelocity * deltaTime);
			m_position.y += static_cast<int>(normalizedY * m_maxVelocity * deltaTime);
		} else if (m_currShotState == exploding) {
			if(m_pAnimationSet->getCurrentAnimationName() == "Exploding" && m_pAnimationSet->getCurrentAnimation()->isFinished()) {
				m_currShotState = finished;
			}
		}

	
	}
	void BaseShot::render(SDL_Surface *const g_destSurface, unsigned int deltaTime) {
		if(m_currShotState != finished) {
			if(m_pAnimationSet != NULL) {
				m_pAnimationSet->render(g_destSurface, deltaTime, m_position);
			}
		}
	}

}